package br.com.bafonline.model.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import br.com.bafonline.model.dao.implementation.GenericDAOImpl;
import br.com.bafonline.model.dto.NegocioDTO;
import br.com.bafonline.util.exception.hibernate.HibernateSessionFactoryException;

/**
 * A data access object (DAO) providing persistence and search support for
 * Negocio entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see br.com.bafonline.model.dto.NegocioDTO
 * @author MyEclipse Persistence Tools
 */

public class NegocioDAO extends GenericDAOImpl {
	private static final Log log = LogFactory.getLog(NegocioDAO.class);
	// property constants
	public static final String TIPO = "tipo";
	public static final String USUARIO = "usuario";
	public static final String FIGURINHA = "figurinha";
	public static final String STATUS = "status";

	public void save(NegocioDTO transientInstance) {
		log.debug("saving Negocio instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		} catch (HibernateSessionFactoryException e) {
			log.error("save failed", e);
		}
	}

	public void delete(NegocioDTO persistentInstance) {
		log.debug("deleting Negocio instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		} catch (HibernateSessionFactoryException e) {
			log.error("delete failed", e);
		}
	}

	public NegocioDTO findById(java.lang.Integer id) {
		log.debug("getting Negocio instance with id: " + id);
		try {
			NegocioDTO instance = (NegocioDTO) getSession().get(
					"br.com.bafonline.model.dto.NegocioDTO", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		} catch (HibernateSessionFactoryException e) {
			log.error("get failed", e);
		}

		return null;
	}

	public List<?> findByExample(NegocioDTO instance) {
		log.debug("finding Negocio instance by example");
		try {
			List<?> results = getSession().createCriteria(
					"br.com.bafonline.model.dto.NegocioDTO").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		} catch (HibernateSessionFactoryException e) {
			log.error("find by example failed", e);
		}

		return null;
	}

	public List<?> findByProperty(String propertyName, Object value) {
		log.debug("finding Negocio instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from NegocioDTO as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		} catch (HibernateSessionFactoryException e) {
			log.error("find by property name failed", e);
		}

		return null;
	}

	public List<?> findByTipo(Object tipo) {
		return findByProperty(TIPO, tipo);
	}

	public List<?> findByUsuario(Object usuario) {
		return findByProperty(USUARIO, usuario);
	}

	public List<?> findByFigurinha(Object figurinha) {
		return findByProperty(FIGURINHA, figurinha);
	}

	public List<?> findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List<?> findAll() {
		log.debug("finding all Negocio instances");
		try {
			String queryString = "from NegocioDTO";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		} catch (HibernateSessionFactoryException e) {
			log.error("find all failed", e);
		}

		return null;
	}

	public NegocioDTO merge(NegocioDTO detachedInstance) {
		log.debug("merging Negocio instance");
		try {
			NegocioDTO result = (NegocioDTO) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		} catch (HibernateSessionFactoryException e) {
			log.error("merge failed", e);
		}

		return null;
	}

	public void attachDirty(NegocioDTO instance) {
		log.debug("attaching dirty Negocio instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		} catch (HibernateSessionFactoryException e) {
			log.error("attach failed", e);
		}
	}

	public void attachClean(NegocioDTO instance) {
		log.debug("attaching clean Negocio instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		} catch (HibernateSessionFactoryException e) {
			log.error("attach failed", e);
		}
	}
}